<?php
namespace app\task\controller;

use app\common\cache\user\IUserCache;
use app\common\enum\AppType;
use app\common\enum\CompanyAccountEvent;
use app\common\enum\CompanyAdminStatus;
use app\common\enum\IsDelete;
use app\common\enum\IsOpen;
use app\common\enum\PoolBidStatus;
use app\common\enum\TenderStatus;
use app\common\enum\UserPoolBidType;
use app\common\ext\ICompanyAccount;
use app\common\ext\IConfig;
use app\common\ext\IDb;
use app\common\ext\IJPush;
use app\common\ext\ISms;

class Bid{
    /**
     * @功能：投标系统自动截止(转待开标状态)
     * @开发者：cxl
     */
    public function bid_stop(){
        //设置php执行时间，直到执行结束为止
        set_time_limit(0);

        //取得待开发的客户
        $user_pool_bid_where['pool_bid_status'] = PoolBidStatus::Bid;
        $user_pool_bid_where['bid_end'] = array("lt",get_date_time());
        $user_pool_bid_list = IDb::getInstance('user_pool_bid')->setDbFiled("user_pool_bid_id,cooperate_admin_id,user_id,bid_num")->setDbWhere($user_pool_bid_where)->sel();
        if($user_pool_bid_list === false){
            exit("Fail");
        }

        //判断数据是否存在
        if(!empty($user_pool_bid_list) && is_array($user_pool_bid_list)){
            //循环结束投标
            foreach($user_pool_bid_list as $item) {
                //修改数据
                $user_pool_bid_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                if($item['bid_num'] > 0){
                    $user_pool_bid_data['pool_bid_status'] = PoolBidStatus::BidDown;
                }else{
                    $user_pool_bid_data['pool_bid_status'] = PoolBidStatus::BidFail;
                    $user_pool_bid_data['open_time'] = get_date_time();
                }

                $user_pool_bid_upd = IDb::getInstance('user_pool_bid')->setDbData($user_pool_bid_data)->setDbWhere($user_pool_bid_where)->upd();
                if ($user_pool_bid_upd === false) {
                    exit("Fail");
                }

                //发送短信
//                $phone = IUserCache::getInfo($item['user_id'],"phone");
//                ISms::Send($phone,ISms::PrTenderStop);

                //发送推送信息
                IJPush::Push(AppType::HZ,"截止抢单！","【有度合作商】您的订单信息已经截止抢单，请尽快去派单；",$item['cooperate_admin_id']);
            }
        }

        //返回值
        exit("Success");
    }

    /**
     * @功能：开标截止系统自动流标
     * @开发者：cxl
     */
    public function tender_open(){
        //设置php执行时间，直到执行结束为止
        set_time_limit(0);

        //取得系统常量
        $OpenBidDate = IConfig::getValue("OpenBidDate");
        $OpenBidDate = intval($OpenBidDate);
        if($OpenBidDate <= 0){
            $OpenBidDate = 24;
        }

        //取得待开发的客户
        $user_pool_bid_where["pool_bid_status"] = PoolBidStatus::BidDown;
        $user_pool_bid_where["date_format((bid_end + interval {$OpenBidDate} hour),'%Y-%m-%d %H:%i:%s')"] = array("elt",get_date_time());
        $user_pool_bid_list = IDb::getInstance('user_pool_bid')->setDbFiled("user_id,user_pool_bid_id")->setDbWhere($user_pool_bid_where)->sel();
        if($user_pool_bid_list === false){
            exit("Fail");
        }

        //判断数据是否存在
        if(!empty($user_pool_bid_list) && is_array($user_pool_bid_list)){
            //开启事物处理
            IDb::dbStartTrans();

            //循环流标
            foreach($user_pool_bid_list as $item) {
                //修改招标状态
                $user_pool_bid_where = null;
                $user_pool_bid_data = null;
                $user_pool_bid_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                $user_pool_bid_data['pool_bid_status'] = PoolBidStatus::BidFail;
                $user_pool_bid_data['open_time'] = get_date_time();
                $user_pool_bid_upd = IDb::getInstance("user_pool_bid")->setDbData($user_pool_bid_data)->setDbWhere($user_pool_bid_where)->upd();
                if($user_pool_bid_upd === false){
                    IDb::dbRollback();
                    exit("Fail");
                }

                //设置未中标信贷经理
                $agent_admin_tender_where = null;
                $agent_admin_tender_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                $agent_admin_tender_list = IDb::getInstance("agent_admin_tender")->setDbFiled("agent_admin_tender_id,agent_admin_id,bond_money")->setDbWhere($agent_admin_tender_where)->sel();
                if ($agent_admin_tender_list === false) {
                    IDb::dbRollback();
                    exit("Fail");
                }

                //设置未中标信贷经理
                $agent_admin_tender_where = null;
                $agent_admin_tender_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                $agent_admin_tender_data['tender_status'] = TenderStatus::NotBid;
                $agent_admin_tender_data['open_time'] = get_date_time();
                $agent_admin_tender_upd = IDb::getInstance("agent_admin_tender")->setDbData($agent_admin_tender_data)->setDbWhere($agent_admin_tender_where)->upd();
                if ($agent_admin_tender_upd === false) {
                    IDb::dbRollback();
                    exit("Fail");
                }

                //修改信贷经理保证金
//                foreach($agent_admin_tender_list as $data){
//                    //取得保证金金额
//                    $bond_money = intval($data['bond_money']);
//
//                    //修改用户资金信息
//                    $account_log = ICompanyAccount::AccountLog($data['agent_admin_id'], CompanyAccountEvent::BondTenderFailure, $bond_money,$data['agent_admin_tender_id']);
//                    if ($account_log === false) {
//                        IDb::dbRollback();
//                        exit("Fail");
//                    }
//                }
            }

            //提交事物处理
            IDb::dbCommit();
        }

        //返回值
        exit("Success");
    }

    /**
     * @功能：开标后系统自动结束招标，退回中标人保证金
     * @开发者：cxl
     */
    private function tender_stop(){
        //设置php执行时间，直到执行结束为止
        set_time_limit(0);

        //取得系统常量
        $StopBidDate = IConfig::getValue("StopBidDate");
        $StopBidDate = intval($StopBidDate);
        if($StopBidDate <= 0){
            $StopBidDate = 15;
        }

        //取得待开发的客户
        $user_pool_bid_where["pool_bid_status"] = PoolBidStatus::BidOpen;
        $user_pool_bid_where["date_format((open_time + interval {$StopBidDate} day),'%Y-%m-%d')"] = array("lt",get_date());
        $user_pool_bid_list = IDb::getInstance('user_pool_bid')->setDbFiled("user_id,user_pool_bid_id")->setDbWhere($user_pool_bid_where)->sel();
        if($user_pool_bid_list === false){
            exit("Fail");
        }

        //判断数据是否存在
        if(!empty($user_pool_bid_list) && is_array($user_pool_bid_list)){
            //开启事物处理
            IDb::dbStartTrans();

            //循环流标
            foreach($user_pool_bid_list as $item) {
                //修改招标状态
                $user_pool_bid_where = null;
                $user_pool_bid_data = null;
                $user_pool_bid_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                $user_pool_bid_data['pool_bid_status'] = PoolBidStatus::BidStop;
                $user_pool_bid_data['stop_time'] = get_date_time();
                $user_pool_bid_upd = IDb::getInstance("user_pool_bid")->setDbData($user_pool_bid_data)->setDbWhere($user_pool_bid_where)->upd();
                if($user_pool_bid_upd === false){
                    IDb::dbRollback();
                    exit("Fail");
                }

                //取得中标人信息
                $agent_admin_tender_where = null;
                $agent_admin_tender_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                $agent_admin_tender_where['tender_status'] = TenderStatus::Bid;
                $agent_admin_tender_list = IDb::getInstance("agent_admin_tender")->setDbFiled("agent_admin_tender_id,agent_admin_id,bond_money")->setDbWhere($agent_admin_tender_where)->sel();
                if ($agent_admin_tender_list === false) {
                    IDb::dbRollback();
                    exit("Fail");
                }

                //设置中标人已经结标
                $agent_admin_tender_where = null;
                $agent_admin_tender_where['user_pool_bid_id'] = $item['user_pool_bid_id'];
                $agent_admin_tender_where['tender_status'] = TenderStatus::Bid;
                $agent_admin_tender_data['tender_status'] = TenderStatus::BidStop;
                $agent_admin_tender_data['stop_time'] = get_date_time();
                $agent_admin_tender_upd = IDb::getInstance("agent_admin_tender")->setDbData($agent_admin_tender_data)->setDbWhere($agent_admin_tender_where)->upd();
                if ($agent_admin_tender_upd === false) {
                    IDb::dbRollback();
                    exit("Fail");
                }

                //修改信贷经理保证金
                foreach($agent_admin_tender_list as $data){
                    //取得保证金金额
                    $bond_money = intval($data['bond_money']);

                    //修改用户资金信息
                    $account_log = ICompanyAccount::AccountLog($item['agent_admin_id'], CompanyAccountEvent::BondTenderRefund, $bond_money,$data['agent_admin_tender_id']);
                    if ($account_log === false) {
                        IDb::dbRollback();
                        exit("Fail");
                    }
                }
            }

            //提交事物处理
            IDb::dbCommit();
        }

        //返回值
        exit("Success");
    }
}
